import {ref} from 'vue'
import {
  BasicColumn
} from '/@/components/Table';
import {
  FormSchema
} from '/@/components/Table';
import {
  rules
} from '/@/utils/helper/validator';
import {
  render
} from '/@/utils/common/renderUtils';
import {
  JVxeTypes,
  JVxeColumn
} from '/@/components/jeecg/JVxeTable/types'
import { queryTreeList } from '/@/api/common/api';

let initTreeList = ref([])
const treeList = ref([])
async function tableData() {
  initTreeList.value = await queryTreeList()
  treeList.value = flatten(initTreeList.value)
}
function flatten(arr) {
  let res = [];
  for (let i = 0; i < arr.length; i++) {
    const child = arr[i].children;
    if (child?.length > 0) {
      res = res.concat(flatten(child));
    }
    res.push(arr[i]);
  }
  return res;
}
tableData()

//列表数据
export const columns: BasicColumn[] = [{
    title: '分组编号',
    align: "left",
    dataIndex: 'code',
    width: 40
  },
  {
    title: '分组名称',
    align: "left",
    dataIndex: 'name',
    width: 40
  },
  {
    title: '资源属性',
    align: "left",
    dataIndex: 'resourceAttribute',
    customRender: ({ text }) => render.renderDict(text, 'resource_attribute'),
    width: 40
  },
  {
    title: '归属部门ID',
    align: "left",
    dataIndex: 'belongDepartmentId',
    ifShow: false
  },
  {
    title: '归属部门',
    align: "left",
    dataIndex: 'belongDepartmentName',
    width: 100,
    customRender:({text}) => treeList.value.filter(item => item.id == text)[0]?.departName
  },
  {
    title: '产品ID',
    align: "left",
    dataIndex: 'materialId',
    ifShow: false
  },
  {
    title: '产品编号',
    align: "left",
    dataIndex: 'materialCode',
    width: 100
  },
  {
    title: '产品名称',
    align: "left",
    dataIndex: 'materialName',
    ifShow:false
  },
  {
    title: '专用产品组',
    align: "left",
    dataIndex: 'specialProductGroup',  
    customRender: ({
      text
    }) => {
      return render.renderDict(text, 'yn');
    },
    width: 40
  },
  {
    title: '备注',
    align: "left",
    dataIndex: 'remark',
    width: 100
  },
  {
    title: '删除状态',
    align: "left",
    dataIndex: 'delFlag',
    ifShow: false
  },
  {
    title: '创建方式',
    align: "left",
    dataIndex: 'createMethod',
    ifShow: false
  },
  {
    title: '第三方编码',
    align: "left",
    dataIndex: 'thirdPartyCode',
    width: 100
  },
  {
    title: '组织ID',
    align: "left",
    dataIndex: 'organizationId',
    ifShow: false
  },
  {
    title: '租户ID',
    align: "left",
    dataIndex: 'tenantId',
    ifShow: false
  },
];
//查询数据
export const searchFormSchema: FormSchema[] = [{
    label: '分组编号',
    field: 'code',
    component: 'Input',
  },
  {
    label: '分组名称',
    field: 'name',
    component: 'Input',
  },
  {
    label: '资源属性',
    field: 'resourceAttribute',
    component: 'JDictSelectTag',
    componentProps: () => {
      return {
        dictCode: 'resource_attribute',
        showChooseOption: false
      }
    }
  }
];
//表单数据
export const formSchema: FormSchema[] = [{
    label: '分组编号',
    field: 'code',
    component: 'Input',
    dynamicRules: ({
      model,
      schema
    }) => {
      return [{
        required: true,
        message: '请输入分组编号!'
      }, ];
    },
  },
  {
    label: '分组名称',
    field: 'name',
    component: 'Input',
    dynamicRules: ({
      model,
      schema
    }) => {
      return [{
        required: true,
        message: '请输入分组名称!'
      }, ];
    },
  },
  {
    label: '资源属性',
    field: 'resourceAttribute',
    component: 'JDictSelectTag',
    defaultValue:0,
    componentProps: {
      dictCode: "resource_attribute",
      stringToNumber: true,
      showChooseOption:false,
    },
    dynamicRules: ({
      model,
      schema
    }) => {
      return [{
        required: true,
        message: '请选择资源属性!'
      }, ];
    },
  },
  {
    label: '归属部门ID',
    field: 'belongDepartmentId',
    component: 'Input',
    ifShow:false,
  },
  {
    label: '归属部门',
    field: 'belongDepartmentName',
    component: 'JSelectDept',
    componentProps:({})=>{
      return{
        multiple: false,
        showButton:false,
        checkable:false,
        rowKey:'id',
        labelKey: 'departName'
      }
    },
  },
  {
    label: '产品ID',
    field: 'materialId',
    component: 'Input',
    ifShow:false
  },
  {
    label: '产品编号',
    field: 'materialCode',
    component: 'Input',
    slot:'materialSlot',
  },
  {
    label: '产品名称',
    field: 'materialName',
    component: 'Input',
    ifShow:false
  },
  {
    label: '专用产品组',
    field: 'specialProductGroup',
    component: 'Checkbox',
  },
  {
    label: '备注',
    field: 'remark',
    component: 'Input',
    colProps:{span:16}
  },
  {
    label: '删除状态',
    field: 'delFlag',
    component: 'InputNumber',
    ifShow:false
  },
  {
    label: '创建方式',
    field: 'createMethod',
    component: 'InputNumber',
    ifShow:false
  },
  {
    label: '第三方编码',
    field: 'thirdPartyCode',
    component: 'Input',
    ifShow:false
  },
  {
    label: '组织ID',
    field: 'organizationId',
    component: 'Input',
    ifShow:false
  },
  {
    label: '租户ID',
    field: 'tenantId',
    component: 'Input',
    ifShow:false
  },
  // TODO 主键隐藏字段，目前写死为ID
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false
  },
];
//子表单数据
//子表表格配置
export const basResourceGroupDefinitionItemColumns: JVxeColumn[] = [{
    title: '资源组定义ID',
    key: 'resourceGroupDefinitionId',
    type: JVxeTypes.hidden,
  },
  {
    title: '关联ID',
    key: 'associationId',
    type: JVxeTypes.hidden,
  },
  {
    title: '资源编码',
    key: 'associationCode',
    type: JVxeTypes.slot,
    slotName: 'associationCodeSlot',
    width: "150px",
    placeholder: '请输入${title}',
    defaultValue: '',
    validateRules: [{
      required: true,
      message: '${title}不能为空'
    }, ],
  },
  {
    title: '资源名称',
    key: 'associationName',
    type: JVxeTypes.normal,
    width: "180px",
    placeholder: '请输入${title}',
    defaultValue: '',
    validateRules: [{
      required: true,
      message: '${title}不能为空'
    }, ],
  },
  {
    title: '优先顺序',
    key: 'priority',
    type: JVxeTypes.inputNumber,
    width: "120px",
    validateRules: [{
      required: true,
      message: '${title}不能为空'
    }, ],
    placeholder: '请输入${title}',
  },
  {
    title: '加工工艺ID',
    key: 'processManagementId',
    type: JVxeTypes.hidden,
    width: "200px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '加工工艺名称',
    key: 'processManagementName',
    type: JVxeTypes.slot,
    slotName: 'processNameSlot',
    width: "200px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '人数',
    key: 'peopleNumber',
    type: JVxeTypes.inputNumber,
    width: "200px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '加工班组ID',
    key: 'useDepartmentId',
    type: JVxeTypes.hidden,
    width: "200px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '加工班组名称',
    key: 'useDepartmentName',
    type: JVxeTypes.slot,
    width: "180px",
    placeholder: '请输入${title}',
    defaultValue: '',
    slotName: 'useDepartmentNameSlot'
  },
  {
    title: '是否专用',
    key: 'specialPurpose',
    type: JVxeTypes.select,
    width: "200px",
    options: [{
      value: 0,
      label: '否'
    }, {
      value: 1,
      label: '是'
    }],
    placeholder: '请选择${title}',
    defaultValue: 0,
  },
  {
    title: '工作时长',
    key: 'workHours',
    type: JVxeTypes.inputNumber,
    width: "200px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '备注',
    key: 'remark',
    type: JVxeTypes.input,
    width: "200px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '删除状态',
    key: 'delFlag',
    type: JVxeTypes.hidden,
    width: "200px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '创建方式',
    key: 'createMethod',
    type: JVxeTypes.hidden,
    width: "200px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '第三方编码',
    key: 'thirdPartyCode',
    type: JVxeTypes.hidden,
    width: "200px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '组织ID',
    key: 'organizationId',
    type: JVxeTypes.hidden,
    width: "200px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '租户ID',
    key: 'tenantId',
    type: JVxeTypes.hidden,
    width: "200px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
]


/**
 * 流程表单调用这个方法获取formSchema
 * @param param
 */
export function getBpmFormSchema(_formData): FormSchema[] {
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}
